package com.neulogistics.contrib.warehouse.api.application.validator;

import cn.hutool.core.util.StrUtil;
import com.neulogistics.contrib.warehouse.api.application.validator.annotation.ValidateAddress;
import com.neulogistics.contrib.warehouse.domain.model.valueobject.Address;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class AddressValidator implements ConstraintValidator<ValidateAddress, Address> {
    @Override
    public boolean isValid(Address value, ConstraintValidatorContext context) {
        // 为空，则放行
        if(value == null)
            return true;

        // 不为空，则所有元素都不能为空,且输入要合法
//        String province = value.getProvince();
//        if(StrUtil.isBlank(province))
//            return false;
//        else {
//            if(province.length() > 12)
//                return false;
//        }

        String city = value.getCity();
        if(StrUtil.isBlank(city))
            return false;
        else {
            if(city.length() > 12)
                return false;
        }

        String zone = value.getZone();
        if(StrUtil.isBlank(zone))
            return false;
        else {
            if(zone.length() > 12)
                return false;
        }

        String detail = value.getDetail();
        if(StrUtil.isBlank(detail))
            return false;
        else {
            if(detail.length() > 30)
                return false;
        }

        return true;
    }
}